package tgc.edu.exam.web.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.UUID;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.exam.custom.CommonController;
import tgc.edu.exam.entity.CurriculumPlan;
import tgc.edu.exam.entity.KcExamination;
import tgc.edu.exam.entity.Student;
import tgc.edu.exam.entity.StudentAssess;
import tgc.edu.exam.entity.SysUser;
import tgc.edu.exam.entity.TbClass;
import tgc.edu.exam.entity.TbCurriculum;
import tgc.edu.exam.security.UserUtils;
import tgc.edu.exam.service.CurriculumPlanService;
import tgc.edu.exam.service.KcExaminationService;
import tgc.edu.exam.service.StudentService;
import tgc.edu.exam.service.TbCurriculumService;
import tgc.edu.exam.service.TeacherService;
import tgc.edu.exam.web.form.KcExaminationForm;

@Controller
@RequestMapping(value="/kcExamination")
public class KcExaminationController extends CommonController<KcExamination, UUID, KcExaminationForm>{

	@Autowired
	private KcExaminationService service;
	@Autowired
	private UserUtils userUtils;
	@Autowired
	private CurriculumPlanService cpService;
	@Autowired
	private TeacherService tService;
	@Autowired
	private StudentService stuService;
	@Autowired
	private TbCurriculumService tbCurriculumService;

	@RequestMapping(value="/tijiao")
	@ResponseBody
	public String tijiao(String id,Double vaule) {
		try {
			if(vaule>=0&&vaule<=100) {
				KcExamination examination = service.findById(UUID.fromString(id));
				examination.setScore(vaule);
				service.save(examination);
				return "数据已保存！";
			}else {
				return "请输入一个百分制分数！";
			}
		} catch (Exception e) {
			return "数据保持失败！";
		}
	}
	
	
	
	@Override
	public void manage(ModelMap map) {
		SysUser user = userUtils.getUser();
		Specification<KcExamination> spec = buildSpec3(user);
		List<KcExamination> kcs = service.findAll(spec);
		Specification<CurriculumPlan> spec1 = buildSpec2(user,kcs);
		List<CurriculumPlan> list = cpService.findAll(spec1);
		for (CurriculumPlan curriculumPlan : list) {
			List<Student> stus = stuService.findByTbClassId(curriculumPlan.getTbClass().getId());
			for(Student stu : stus) {
				KcExamination exam = new KcExamination();
				exam.setKcuuid(curriculumPlan.getCurriculum().getId().toString());
				exam.setKcname(curriculumPlan.getCurriculum().getCourseName());
				exam.setBanjiid(stu.getTbClass().getId().toString());
				exam.setBanji(stu.getTbClass().getName());
				exam.setTeacher(tService.findById(user.getId()));
				exam.setStudent(stu);
				service.save(exam);
			}
		}
		List<String> kcuuid = service.findKcuuid();
		List<TbCurriculum> tbcurriculum = new ArrayList<>();
		for (String kcid : kcuuid) {
			TbCurriculum curriculum = tbCurriculumService.findById(UUID.fromString(kcid));
			tbcurriculum.add(curriculum);
		}
		map.put("data", tbcurriculum);
		super.manage(map);
	}

	private Specification<KcExamination> buildSpec3(SysUser user) {
		Specification<KcExamination> spec = new Specification<KcExamination>() {

			@Override
			public Predicate toPredicate(Root<KcExamination> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules = new HashSet<>();
				
					rules.add(cb.equal(root.get("teacher").get("id"), user.getId()));
				
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		return spec;
	}

	private Specification<CurriculumPlan> buildSpec2(SysUser user, List<KcExamination> kcs) {
		Specification<CurriculumPlan> spec = new Specification<CurriculumPlan>() {

			@Override
			public Predicate toPredicate(Root<CurriculumPlan> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules = new HashSet<>();
				
					rules.add(cb.equal(root.get("teacher").get("id"), user.getId()));
					String kcuuid2 = "";
					for (KcExamination kcExamination : kcs) {
						String kcuuid = kcExamination.getKcuuid();
						if(!kcuuid.equals(kcuuid2)) {
							rules.add(cb.notEqual(root.get("curriculum").get("id"), UUID.fromString(kcuuid)));
							kcuuid2 = kcuuid;
						}
					}
				
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		
		return spec;
	}

	@Override
	public Specification<KcExamination> buildSpec(KcExaminationForm form) {
		Specification<KcExamination> spec = new Specification<KcExamination>() {

			@Override
			public Predicate toPredicate(Root<KcExamination> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules = new HashSet<>();
				String kcuuid = form.getKcuuid();
				if(StringUtils.hasText(kcuuid)) {	
				rules.add(cb.equal(root.get("kcuuid"), kcuuid));
				}
				
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		return spec;
	}
	
	
}
